package org.openkoala.koala.popup.javaclass.crud;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

import org.openkoala.koala.deploy.curd.module.core.FieldModel;
import org.openkoala.koala.deploy.curd.module.core.RelationModel;
import org.openkoala.koala.deploy.curd.module.core.RelationType;
import org.openkoala.koala.deploy.curd.module.core.model.RelationFieldModel;
import org.openkoala.koala.deploy.curd.module.core.relation.ManyToManyRelationModel;
import org.openkoala.koala.deploy.curd.module.core.relation.ManyToOneRelationModel;
import org.openkoala.koala.deploy.curd.module.core.relation.OneToManyRelationModel;
import org.openkoala.koala.deploy.curd.module.core.relation.OneToOneRelationModel;
import org.openkoala.koala.deploy.curd.module.ui.Model;
import org.openkoala.koala.deploy.curd.module.ui.UIWidget;
import org.openkoala.koala.deploy.curd.module.ui.model.AddModel;
import org.openkoala.koala.deploy.curd.module.ui.model.ListModel;
import org.openkoala.koala.deploy.curd.module.ui.model.QueryModel;
import org.openkoala.koala.deploy.curd.module.ui.model.TabModel;
import org.openkoala.koala.deploy.curd.module.ui.model.TabViewUI;
import org.openkoala.koala.deploy.curd.module.ui.model.UpdateModel;
import org.openkoala.koala.deploy.curd.module.ui.model.ViewModel;
import org.openkoala.koala.deploy.curd.module.ui.view.DateView;
import org.openkoala.koala.deploy.curd.module.ui.view.HiddenView;
import org.openkoala.koala.deploy.curd.module.ui.view.QueryDropView;
import org.openkoala.koala.deploy.curd.module.ui.view.ReadOnlyView;
import org.openkoala.koala.deploy.curd.module.ui.view.RelativeReadOnlyView;
import org.openkoala.koala.deploy.curd.module.ui.view.StaticDropView;
import org.openkoala.koala.deploy.curd.module.ui.view.TextAreaView;
import org.openkoala.koala.deploy.curd.module.ui.view.TextView;

/**
 * 
 * 类    名：CrudUtils.java
 *   
 * 功能描述：CRUD配置的工具类。	
 *  
 * 创建日期：2013-1-29下午5:09:02     
 * 
 * 版本信息：
 * 
 * 版权信息：Copyright (c) 2013 Koala All Rights Reserved
 * 
 * 作    者：xmfang(xinmin.fang@gmail.com)
 * 
 * 修改记录： 
 * 修 改 者    修改日期     文件版本   修改说明
 */
public class CrudUtils {

	/**
	 * 根据条件生成UIWidget实例
	 * @param fieldName
	 * @param viewName
	 * @param editType
	 * @return
	 */
	public static UIWidget createEditUIWidgetBy(String fieldName, String viewName, String express, String fieldType, EditType editType, Map<String, String> dropData) {
		if (express == null) {
			express = fieldName;
		}
		
		if (editType == EditType.TEXT_FIELD) {
			TextView textView = new TextView();
			textView.setName(viewName);
			textView.setExpress(express);
			textView.setType(fieldType);
			return textView;
		}
		if (editType == EditType.TEXT_AREA) {
			TextAreaView textAreaView = new TextAreaView();
			textAreaView.setName(viewName);
			textAreaView.setExpress(express);
			textAreaView.setType(fieldType);
			return textAreaView;
		}
		if (editType == EditType.DATE_PICKER) {
			DateView dateView = new DateView();
			dateView.setName(viewName);
			dateView.setExpress(express);
			dateView.setType(fieldType);
			return dateView;
		}
		if (editType == EditType.COMBO_BOX) {
			StaticDropView staticDropView = new StaticDropView();
			staticDropView.setName(viewName);
			staticDropView.setExpress(express);
			staticDropView.setType(fieldType);
			staticDropView.setValueMap(dropData);
			return staticDropView;
		}
		if (editType == EditType.ENTITY_COMBO_BOX) {
			QueryDropView queryDropView = new QueryDropView();
			queryDropView.setName(viewName);
			queryDropView.setExpress(express);
			queryDropView.setType(fieldType);
			return queryDropView;
		}
		if (editType == EditType.HIDDEN_FIELD) {
			HiddenView hiddenView = new HiddenView();
			hiddenView.setName(viewName);
			hiddenView.setExpress(express);
			hiddenView.setType(fieldType);
			return hiddenView;
		}
		
		return null;
	}

	/**
	 * 根据条件生成ReadOnlyView实例
	 * @param fieldName
	 * @param viewName
	 * @param fieldType
	 * @return
	 */
	public static UIWidget createReadOnlyView(String fieldName, String viewName, String express, String fieldType) {
		if (express == null) {
			express = fieldName;
		}
		
		ReadOnlyView readOnlyView = new ReadOnlyView();
		readOnlyView.setName(viewName);
		readOnlyView.setExpress(express);
		readOnlyView.setType(fieldType);
		return readOnlyView;
	}

	/**
	 * 根据条件生成RelativeReadOnlyView实例
	 * @param fieldName
	 * @param viewName
	 * @param fieldType
	 * @return
	 */
	public static UIWidget createRelativeReadOnlyView(String fieldName, String viewName, String express, String fieldType) {
		if (express == null) {
			express = fieldName;
		}
		
		RelativeReadOnlyView relativeReadOnlyView = new RelativeReadOnlyView();
		relativeReadOnlyView.setName(viewName);
		relativeReadOnlyView.setRelative(fieldName);
		relativeReadOnlyView.setExpress(express);
		relativeReadOnlyView.setType(fieldType);
		return relativeReadOnlyView;
	}
	
	/**
	 * 判断fieldName的属性是否已经存在于model中
	 * @param fieldName
	 * @param model
	 * @return
	 */
	public static boolean fieldNameIsInModel(String fieldName, Model model) {
		for (UIWidget widget : model.getViews()) {
			if (widget instanceof RelativeReadOnlyView) {
				if (((RelativeReadOnlyView) widget).getRelative().equals(fieldName)) {
					return true;
				}
				continue;
			}
			if (widget.getExpress().equals(fieldName)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 根据UIWiget返回相应的EditType描述
	 * @param widget
	 * @return
	 */
	public static String getEditTypeNameByUIWiget(UIWidget widget) {
		if (widget instanceof TextView) {
			return EditType.TEXT_FIELD.getDescription();
		}
		if (widget instanceof TextAreaView) {
			return EditType.TEXT_AREA.getDescription();
		}
		if (widget instanceof DateView){
			return EditType.DATE_PICKER.getDescription();
		}
		if (widget instanceof StaticDropView) {
			return EditType.COMBO_BOX.getDescription();
		}
		if (widget instanceof QueryDropView) {
			return EditType.ENTITY_COMBO_BOX.getDescription();
		}
		if (widget instanceof HiddenView) {
			return EditType.HIDDEN_FIELD.getDescription();
		}
		
		return null;
	}
    
	/**
	 * 根据UIWiget返回相应的关联属性名
	 * @param widget
	 * @return
	 */
	public static String getAssociationPropertyNameByUIWiget(UIWidget widget) {
		if (widget instanceof ReadOnlyView) {
			return ((ReadOnlyView) widget).getExpress();
		}
		
		if (widget instanceof RelativeReadOnlyView) {
			RelativeReadOnlyView relativeReadOnlyView = (RelativeReadOnlyView) widget;
			return relativeReadOnlyView.getRelative() + "." + relativeReadOnlyView.getExpress();
		}
		
		return null;
	}
	
	/**
	 * 根据model返回具体的关联类型
	 * @param relationModel
	 * @return
	 */
	public static RelationType getRelationTypeByModel(RelationModel relationModel) {
		if (relationModel instanceof ManyToOneRelationModel) {
			return RelationType.ManyToOne;
		}
		if (relationModel instanceof ManyToManyRelationModel) {
			return RelationType.ManyToMany;
		}
		if (relationModel instanceof OneToManyRelationModel) {
			return RelationType.OneToMany;
		}
		if (relationModel instanceof OneToOneRelationModel) {
			return RelationType.OneToOne;
		}
		return null;
	}
	
	/**
	 * 根据model获取model的类型
	 * @param model
	 * @return
	 */
    public static String getModelType(Model model) {
        if (model instanceof AddModel || model instanceof QueryModel || model instanceof UpdateModel) {
        	return CrudConstants.MODEL_EDIT;
        }
        if (model instanceof ListModel || model instanceof ViewModel) {
        	return CrudConstants.MODEL_VIEW;
        }
        if (model instanceof TabModel) {
        	return CrudConstants.MODEL_ASSOCIATION;
        }
        return null;
    }
	
    public static FieldModel getFieldModelByName(String fieldName, Collection<FieldModel> fieldModels) {
		for (FieldModel fieldModel : fieldModels) {
			if (fieldModel.getName().equals(fieldName)) {
				return fieldModel;
			}
		}
		return null;
	}
	
	public static TabViewUI getTabViewUiFromModel(RelationFieldModel relationFieldModel, ViewModel viewModel) {
		for (TabViewUI tabViewUI : viewModel.getTabs()) {
			if (tabViewUI.getRelationFieldModel().equals(relationFieldModel)) {
				return tabViewUI;
			}
		}
		return null;
	}

	/**
	 * 根据属性类型检查属性是否是关联属性
	 * @param fieldType
	 * @return
	 */
	public static boolean isAssociationType(String fieldType) {
		if (isPrimitiveType(fieldType)) {
			return false;
		}
		
		try {
			return !CrudUtils.isBaseType(Class.forName(fieldType));
		} catch (ClassNotFoundException e) {
			return true;
		}
	}
	
	private static boolean isPrimitiveType(String fieldType) {
		return (fieldType.equals("int") ||  
				fieldType.equals("float") ||  
				fieldType.equals("long") ||  
				fieldType.equals("double") ||  
				fieldType.equals("short") ||  
				fieldType.equals("byte"));
	}
	
	private static boolean isBaseType(Class<?> clazz) {
		return (clazz.equals(String.class) ||  
				clazz.equals(Integer.class) ||  
				clazz.equals(Byte.class) ||  
				clazz.equals(Long.class) ||  
				clazz.equals(Double.class) ||  
				clazz.equals(Float.class) ||  
				clazz.equals(Character.class) ||  
				clazz.equals(Short.class) ||  
				clazz.equals(BigDecimal.class) ||  
				clazz.equals(BigInteger.class) ||  
				clazz.equals(Boolean.class) ||  
				clazz.equals(Date.class) ||  
				clazz.isPrimitive());
	}
}
